﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.SessionState;
using PreguntaAlExperto.RulesLayer.Services;
using PreguntaAlExperto.RulesLayer.Entities;

namespace PreguntaAlExperto
{
    public class KeepMeInform
        : IHttpHandler, IRequiresSessionState
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            if (!string.IsNullOrEmpty(context.Request.QueryString["ST"]))
            {
                if (!string.IsNullOrEmpty(context.Request.QueryString["URL"]))
                {
                    string _URL = context.Request.QueryString["URL"].ToString();
                    ArticlesService _service = new ArticlesService();
                    Article _article = _service.getArticleByPath(_URL);
                    bool _action = context.Request.QueryString["ST"].ToString() == "1" ? true : false;
                    bool _value = _service.MarkForFollowArticle(_article.Article_ID, int.Parse(context.Session["UserID"].ToString()), _action);
                    if (_value)
                        context.Response.Write("1");
                    else
                        context.Response.Write("0");
                }
                else
                {
                    context.Response.Write("0");
                }
            }
            else
            { 
                //Return de actual value
                string _URL = context.Request.QueryString["URL"].ToString();
                ArticlesService _service = new ArticlesService();
                Article _article = _service.getArticleByPath(_URL);
                bool _value = _service.GetFollowArticleStatus(_article.Article_ID, int.Parse(context.Session["UserID"].ToString()));
                if (_value)
                    context.Response.Write("1");
                else
                    context.Response.Write("0");

            }
        }

        #endregion
    }
}